//
//  LHUserTableView.m
//  Knight_Tour
//
//  Created by dlios on 15-5-14.
//  Copyright (c) 2015年 TouchOne. All rights reserved.
//

#import "LHUserTableView.h"
#import "LHUserTableViewCell.h"
#import "LHUserOtherTableViewCell.h"

static CGFloat kImageOriginHight = 220;
static CGFloat kTempHeight = 80.0f;

@implementation LHUserTableView

- (void)dealloc
{
    [_tableView release];
    [_headImageView release];
    [_userArr release];
    [super dealloc];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.userArr = [@[@"我的游记", @"我的景点", @"我的推荐景点", @"我的旅店", @"我的餐馆", @"清除缓存", @"关于我们"]mutableCopy];
        [self createTableView];
        [self topView];
    }
    return self;
}

// 创建tableView
- (void)createTableView
{
//    self.headImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, -kImageOriginHight - kTempHeight, self.tableView.frame.size.width, kImageOriginHight + kTempHeight )];
   // 2.创建一个ImageView
    self.headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,  0, self.bounds.size.width , self.bounds.size.height / 2)];
    self.headImageView.image = [UIImage imageNamed:@"01"];
    self.headImageView.contentMode = UIViewContentModeScaleAspectFill;
    // 3.把imageView作为子视图放到tableView的首位
    [self addSubview:self.headImageView];
    //    [self.tableView addSubview:_headImageView];

    [_headImageView release];
    // 创建UITableView
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.rowHeight = 50;
    self.tableView.backgroundColor = [UIColor clearColor];
    [self addSubview:_tableView];
    [_tableView release];
    
    // 注册cell
    [self.tableView registerClass:[LHUserOtherTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.tableView registerClass:[LHUserTableViewCell class] forCellReuseIdentifier:@"user"];
    _tableView.tableFooterView = [[UIView alloc] init];
//    // 4.设置tableView的内边距, 让cell下移imageView高度的一半
    _tableView.contentInset = UIEdgeInsetsMake(self.headImageView.frame.size.height, 0, 0, 0);
}
- (void)topView
{
    self.lable1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width / 4, 20)];
    //        _lable1.backgroundColor = [UIColor redColor];
    _lable1.center = CGPointMake(self.frame.size.width/2, self.frame.size.height / 3 * 1);
    _lable1.text = @"我的收藏";
    _lable1.textAlignment = 1;
    [_lable1 setTextColor:[UIColor whiteColor]];
    [self.tableView addSubview:_lable1];
    [_lable1 release];
    
    self.lable2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width / 5 * 4, 20)];
    //        _lable2.backgroundColor = [UIColor yellowColor];
    _lable2.text =@"记录旅行的足迹,丰富世界的风景";
    _lable2.center = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 5 * 2);
    [_lable2 setTextColor:[UIColor colorWithRed:62 % 256 / 255.0 green:62 % 256 / 255.0   blue:62 % 256 / 255.0 alpha:1] ];
    _lable2.textAlignment = 1;
    [self.tableView addSubview:_lable2];
    [_lable2 release];
    
    self.image1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width / 7, self.frame.size.width / 7)];
    _image1.image = [UIImage imageNamed:@"02"];
    _image1.center = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 4 * 1);
    //        _image1.backgroundColor = [UIColor grayColor];
    //        _image1.alpha = 0.7;
    [_image1.layer setCornerRadius:25];
    [self.tableView addSubview:_image1];
    [_image1 release];
}
// 重写 scrollView 的协议
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{

//    CGFloat yOffset  = scrollView.contentOffset.y;
//    NSLog(@"yOffset===%f",yOffset);
//    CGFloat xOffset = (yOffset + kImageOriginHight)/2;
//    if (yOffset < -kImageOriginHight) {
//        CGRect f = self.headImageView.frame;
//        f.origin.y = yOffset - kImageOriginHight;
//        f.size.height =  -yOffset + kImageOriginHight;
//        f.origin.x = xOffset;
//        f.size.width = self.frame.size.width + fabsf(xOffset)*2;
//        self.headImageView.frame = f;
//    }
//    CGFloat down = - (self.bounds.size.height * 1.5) - scrollView.contentOffset.y;
//    if (down < 0) return;
//    NSLog(@"bilibili%f",down);
//    CGRect frame = self.headImageView.frame;
//    frame.size.height = self.bounds.size.height + down * 2;
//    self.headImageView.frame = frame;
    
}
// 实现协议方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.row == 0) {
        LHUserTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"user" forIndexPath:indexPath];
        cell.lable1.text = _userArr[indexPath.row];
        cell.image1.image = [UIImage imageNamed:@"coffe4"];
        return cell;
    } else if (indexPath.row == 1) {
        LHUserTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"user" forIndexPath:indexPath];
        cell.lable1.text = _userArr[indexPath.row];
        cell.image1.image = [UIImage imageNamed:@"coffe1"];
        return cell;
    } else if (indexPath.row == 2) {
        LHUserOtherTableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        cell1.lable1.text = _userArr[indexPath.row];
        cell1.image1.image = [UIImage imageNamed:@"coffe5"];
        return cell1;
    } else if (indexPath.row == 3) {
        LHUserOtherTableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        cell1.lable1.text = _userArr[indexPath.row];
        cell1.image1.image = [UIImage imageNamed:@"coffe"];
        return cell1;
    } else if (indexPath.row == 4) {
        LHUserTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"user" forIndexPath:indexPath];
        cell.lable1.text = _userArr[indexPath.row];
        cell.image1.image = [UIImage imageNamed:@"coffe2"];
        return cell;
    } else if (indexPath.row == 5){
        LHUserTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"user" forIndexPath:indexPath];
    cell.lable1.text = _userArr[indexPath.row];
    cell.image1.image = [UIImage imageNamed:@"coffe2"];
        return cell;
    } else if (indexPath.row == 6){
        LHUserTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"user" forIndexPath:indexPath];
        cell.lable1.text = _userArr[indexPath.row];
        cell.image1.image = [UIImage imageNamed:@"coffe2"];
    return cell;
    }
    return nil;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _userArr.count;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.delegate jumpToDetailPage:indexPath];
    [_tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}
@end
